<template>
  <view class="order-history-container bg-theme">
    <view class="order-history-list" v-if="!isEmpty">
      <order-card v-for="(item, index) in list" :key="index" :info="item">
        <block slot="head-right">
          <span
            >{{ item.status_name }} <u-icon name="arrow-right"></u-icon
          ></span>
        </block>
      </order-card>
      <view class="loadmore">
        <u-loading size="36" :show="loading"></u-loading>
      </view>
    </view>
    <view v-else style="min-height: 500rpx; position: relative">
      <ex-empty></ex-empty>
    </view>
  </view>
</template>

<script>
import OrderCard from "@/components/trade/buy/OrderCard";
export default {
  name: "OrderHistory",
  components: { OrderCard },
  props: {
    matchId: {
      type: String | Number,
      default: 0,
    },
  },
  data() {
    return {
      list: [],
      loading: false,
      pagination: {
        page: 1,
        limit: 20,
        total: 0,
      },
      statusMap: {
        "It's all done": this.$t("orderCard.status[1]"),
        rescinded: this.$t("orderCard.status[2]"),
      },
    };
  },
  computed: {
    isEmpty() {
      return this.$u.test.isEmpty(this.list);
    },
  },
  mounted() {
    this.fetchList();
  },
  methods: {
    async fetchList() {
      const path = "/tx_history/list";
      const params = {
        currency_match_id: this.matchId,
        page: this.pagination.page,
        limit: this.pagination.limit,
      };
      this.loading = true;
      const res = await this.$u.get(path, params);
      this.loading = false;
      if (res.code == 1) {
        const data = res.data;
        let list = data.data;
        this.pagination.total = data.total;
        if (Array.isArray(list) && list.length > 0) {
          if (this.pagination.page == 1) {
            this.list = list;
          } else {
            this.list.push(...list);
          }
        }
      }
    },
    reload() {
      this.page = 1;
      this.fetchList();
    },
    fetchMore() {
      if (
        this.pagination.page * this.pagination.limit <
        this.pagination.total
      ) {
        this.pagination.page++;
        this.fetchList();
      }
    },
  },
};
</script>

<style lang="less" scoped>
@import "~@/assets/styles/mixins.less";

.loadmore {
  display: flex;
  justify-content: center;
  align-items: center;
  padding-top: 10px;
  .safe-padding-bottom(10px);
}
</style>
